package webdev.adapters;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import dev.danang.R;

public class ImagePostAdapter extends BaseAdapter{

	private ArrayList<Bitmap> mData;
	private LayoutInflater inflater = null;
	private Activity activity;

	public ImagePostAdapter(Activity a, ArrayList<Bitmap> d) {
		 activity = a;
		 mData = d;
		 inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return mData.size();
	}

	@Override
	public Bitmap getItem(int position) {
		 return mData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View row = convertView;
		ViewHolder mViewHolder;
		Bitmap image = getItem(position);
		if (row == null) {
			 row = inflater.inflate(R.layout.custom_listview_image_post_photo, null);
			 mViewHolder = new ViewHolder();
			 mViewHolder.mImageView = (ImageView)row.findViewById(R.id.image);
			 mViewHolder.btn_remove_photo = (ImageButton)row.findViewById(R.id.btn_remove_photo);
			 
			 row.setTag(mViewHolder);
		} else {
			mViewHolder = (ViewHolder)row.getTag();
		}
		
		mViewHolder.btn_remove_photo.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mData.remove(position);
				notifyDataSetChanged();
			}
		});
		
		mViewHolder.mImageView.setImageBitmap(image);
		return row;
	}

	private class ViewHolder {
		ImageView mImageView;
		ImageButton btn_remove_photo;
	}
}
